# ==========================Basic Settings========================
# 编译工具
HOST := 
CC := $(HOST)gcc
AR := $(HOST)ar
LD := $(HOST)ld
# 库路径及链接库
LDFLAGS = -L.
LIBS = 
ifeq ($(OS), Windows_NT)
	LIBS += -lpthread
else 
	LIBS += -lpthread
endif
# 输出目标名（最后输出的可执行文件名）
TARGET = pkgframe
# 输出目标存放的路径（需要输出可执行文件存放的路径）
TARGET_FOLDER = .
# 生成库文件的目录
LIBS_FOLDER = .
# 源文件路径(只支持一个目录)
SRC_PATH = ./src
# 头文件路径(根据需要增删路径)
INCLUDES = ./src
# 定义全局宏(根据需要增删宏)
DEFINES += LOG_EN MULTIPLY_THREADS_SUPPORT


#=======================advanced setting=============================
# 编译参数
CFLAGS = -fPIC -Wall -std=c99 -c
# 安装路径(安装跟卸载最好自行实现)
PREFIX = ./install
# service跟脚本路径
SERVICE_PATH = ./system
# 生成.o文件的目录
OBJ_FOLDER = ./obj
# 指定程序库选项（在 LIBS 中添加 [例： LIBS += -lpthread]）
ifeq ($(OS), Windows_NT)	# 设置程序运行后自动寻找库的路径，冒号隔开多个路径
# not support
else 
# LIBS += -Wl,-rpath=./src/lib1:./src/lib2
# LIBS += -Wl,-rpath=/path/to/library1:/path/to/library2
# LIBS += -Wl,-rpath -Wl,LIBDIR(兼容性写法)
endif
# 设置链接时是链接动态库还是静态库
# LIBS += -Wl,-Bstatic -lmylibrary -Wl,-Bdynamic
#============================================================

# set library name
ifeq ($(OS), Windows_NT)
	TARGETSO = $(TARGET).dll
	TARGETA = $(TARGET).lib
else 
	TARGETSO = lib$(TARGET).so
	TARGETA = lib$(TARGET).a
endif

SOURCE_FILES_C= $(wildcard $(SRC_PATH)/*.c)
SOURCE_FILES_CPP= $(wildcard $(SRC_PATH)/*.cpp)

INCLUDE_PREFIX=-I
ALL_INCLUDES=$(addprefix $(INCLUDE_PREFIX), $(INCLUDES))

DEFINE_PREFIX=-D
ALL_DEFINES=$(addprefix $(DEFINE_PREFIX), $(DEFINES))

# get obj file
OBJ_FILES_C = $(patsubst $(SRC_PATH)/%.c,$(OBJ_FOLDER)/%.o,$(SOURCE_FILES_C))
OBJ_FILES_CPP = $(patsubst $(SRC_PATH)/%.cpp,$(OBJ_FOLDER)/%.o,$(SOURCE_FILES_CPP))
OBJ_FILES = $(OBJ_FILES_C) $(OBJ_FILES_CPP)
ALL_OBJ_DEPDEND = $(patsubst %.o, %.o.d, $(OBJ_FILES))
OBJ_FILES_LIB = $(filter-out $(OBJ_FOLDER)/main.o, $(OBJ_FILES))

.PHONY : clean lib install service uninstall showlibpary prep
.DEFAULT_GOAL := $(TARGET)

$(TARGET): $(OBJ_FILES)
	@echo target run
	$(CC) $(LDFLAGS) -o $(TARGET_FOLDER)/$@ $^ $(LIBS)

$(OBJ_FOLDER)/%.o : $(SRC_PATH)/%.c | prep
	$(CC) -MMD -MF $(OBJ_FOLDER)/$(notdir $@).d -MT $@ $(ALL_DEFINES) $(ALL_INCLUDES) $(CFLAGS) $< -o $@

$(OBJ_FOLDER)/%.o : $(SRC_PATH)/%.cpp | prep
	$(CC) -MMD -MF $(OBJ_FOLDER)/$(notdir $@).d -MT $@ $(ALL_DEFINES) $(ALL_INCLUDES) $(CFLAGS) $< -o $@

-include $(ALL_OBJ_DEPDEND)

SERVICE_PATH_INSTALL :=
ifeq ($(shell test -d /usr/lib/systemd/system && echo true),true)  
	SERVICE_PATH_INSTALL := /usr/lib/systemd/system
else  
ifeq ($(shell test -d /lib/systemd/system && echo true),true)  
	SERVICE_PATH_INSTALL := /lib/systemd/system
else  
# 如果上面两个地址都不存在，则在下方自行填写适合的位置	
endif  
endif  


prep:
	@echo prep run
	@if test ! -d $(OBJ_FOLDER); then mkdir -p $(OBJ_FOLDER); fi
	@if test ! -d $(TARGET_FOLDER); then mkdir -p $(TARGET_FOLDER); fi
	@if test ! -d $(LIBS_FOLDER); then mkdir -p $(LIBS_FOLDER); fi

# show for debug
show:
	@echo "SOURCE_FILES_C: $(SOURCE_FILES_C)"
	@echo "SOURCE_FILES_CPP: $(SOURCE_FILES_CPP)"
	@echo "OBJ_FILES_C: $(OBJ_FILES_C)"
	@echo "OBJ_FILES_LIB: $(OBJ_FILES_LIB)"
	@echo "OBJ_FILES: $(OBJ_FILES)"
	@echo "ALL_OBJ_DEPDEND: $(ALL_OBJ_DEPDEND)"

showlibpary:
	@echo 'export PATH=$$PATH:$$PWD'
	@echo 'export LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$$PWD'

lib: prep $(OBJ_FILES)
	$(CC) -shared -fPIC $(LDFLAGS) -o $(LIBS_FOLDER)/$(TARGETSO) $(OBJ_FILES_LIB) $(LIBS)
	$(AR) rcs $(LIBS_FOLDER)/$(TARGETA) $(OBJ_FILES_LIB)

install: $(TARGET) lib
	@if test ! -d $(PREFIX); then mkdir -p $(PREFIX); fi
	@if test ! -d $(PREFIX)/bin; then mkdir -p $(PREFIX)/bin; fi
	@if test ! -d $(PREFIX)/lib; then mkdir -p $(PREFIX)/lib; fi
	@if test ! -d $(PREFIX)/include; then mkdir -p $(PREFIX)/include; fi
	cp $(TARGET_FOLDER)/$(TARGET) $(PREFIX)/bin
	cp $(LIBS_FOLDER)/$(TARGETSO) $(LIBS_FOLDER)/$(TARGETA) $(PREFIX)/lib
	cp $(INCLUDES)/pkgframe.h $(PREFIX)/include

uninstall:
	rm -f $(PREFIX)/bin/$(TARGET)
	rm -f $(PREFIX)/lib/$(TARGETSO) $(PREFIX)/lib/$(TARGETA)
	rm -f $(PREFIX)/include/pkgframe.h

clean:
	rm -f $(OBJ_FOLDER)/*.o $(OBJ_FOLDER)/*.d
	rm -f $(TARGET_FOLDER)/$(TARGET) 
	rm -f $(LIBS_FOLDER)/$(TARGETSO) $(LIBS_FOLDER)/$(TARGETA)
